package evi.com.evi.activity;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import evi.com.evi.R;
import evi.com.evi.adapter.OrderDisposeAdapter;
import evi.com.evi.adapter.SearchRecordAdapter;
import evi.com.evi.application.MyApplication;
import evi.com.evi.utils.ScreenUtils;

/**
 * 搜索
 *
 * @author MY
 */
public class SearchActivity extends Activity implements OnClickListener {

    private EditText et_search;
    private ImageView iv_search;
    private TextView tv_cancle;

    private ListView lv_business;
    private OrderDisposeAdapter mAdapter;

    private LinearLayout ll_null_data;

    private LinearLayout ll_record;
    private TextView tv_clear;
    private ListView lv_record;
    private SearchRecordAdapter RecordAdapter;
    private ArrayList<String> recordList;


    private int ActType = 0;    //1-未处理， 2-已处理

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ScreenUtils.setTranStatus(this, false, Color.parseColor("#FF6600"));
        MyApplication.getInstance().addActivity(this);
        setContentView(R.layout.activity_search);

        ActType = getIntent().getIntExtra("ActType", 0);

        initView();
    }

    private void initView() {
        et_search = (EditText) findViewById(R.id.et_search);
        iv_search = (ImageView) findViewById(R.id.iv_search);
        tv_cancle = (TextView) findViewById(R.id.tv_cancle);
        lv_business = (ListView) findViewById(R.id.lv_business);
        ll_null_data = (LinearLayout) findViewById(R.id.ll_null_data);

        ll_record = (LinearLayout) findViewById(R.id.ll_record);
        tv_clear = (TextView) findViewById(R.id.tv_clear);
        lv_record = (ListView) findViewById(R.id.lv_record);

        iv_search.setOnClickListener(this);
        tv_cancle.setOnClickListener(this);
        tv_clear.setOnClickListener(this);

        et_search.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    et_search.setHint(null);
                    showRecord();
                } else {
                    et_search.setHint("");
                    showInfo(ActType);
                }
            }

        });
        et_search.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                showRecord();
            }
        });

        recordList = new ArrayList<String>();
        recordList.add("1111");
        recordList.add("2222");
        recordList.add("3333");
        recordList.add("4444");
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_cancle:        //取消
                finish();
                break;

            case R.id.iv_search:        //搜索
                searchData("");
                break;

            case R.id.tv_clear:        //清除
                //清除历史记录...

                showRecord();
                break;

            default:
                break;
        }
    }

    public void searchData(String text) {
        //执行搜索....

        showInfo(ActType);
    }

    private void showInfo(int type) {
        ll_null_data.setVisibility(View.GONE);
        lv_business.setVisibility(View.VISIBLE);
        ll_record.setVisibility(View.GONE);

        ArrayList<String> list = new ArrayList<String>();
        list.add("1");
        list.add("1");
        list.add("1");
        list.add("1");

        mAdapter = new OrderDisposeAdapter(SearchActivity.this, list, type);
        lv_business.setAdapter(mAdapter);
    }

    private void showRecord() {
        ll_null_data.setVisibility(View.GONE);
        lv_business.setVisibility(View.GONE);
        ll_record.setVisibility(View.VISIBLE);

        RecordAdapter = new SearchRecordAdapter(SearchActivity.this, recordList);
        lv_record.setAdapter(RecordAdapter);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        MyApplication.getInstance().deleteActivity(this);
    }
}